package com.nation.controller;

import com.nation.domain.TNatureAndResource;
import com.nation.domain.User;
import com.nation.domain.VInternationalAndRelationship;
import com.nation.domain.VOverseasChinese;
import com.nation.service.OverseasService;
import com.nation.service.RelationshipService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.apache.xpath.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/4.
 */
@Controller
@RequestMapping(value = "/OverseasChinese")
public class OverseasController {
    @Autowired
    private OverseasService OverseasService;

    /**
     * 侨情概况
     * @param map
     * @return
     */
    @RequestMapping(value = "/overseaschinese")
    public String development(ModelMap map){
        List<VOverseasChinese> olist=OverseasService.findAll();
        map.put("olist",olist);
        return "OverseasChinese/overseaschinese";
    }

    /**
     * 修改页面
     * @param country_code
     * @param map
     * @return
     */
    @RequestMapping(value = "/overseaschinese_update")
    public String development_update(String country_code,ModelMap map){
        VOverseasChinese overseaschinese=OverseasService.findByCountryCode(country_code);
        map.put("overseaschinese",overseaschinese);
        return "OverseasChinese/overseaschinese-update";
    }

    /**
     * 修改信息具体动作
     * @param VOverseasChinese
     * @return
     */
    @RequestMapping(value = "/overseaschinese_update.do")
    @ResponseBody
    public String development_pupdate_do(VOverseasChinese VOverseasChinese){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VOverseasChinese.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=OverseasService.update(VOverseasChinese);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/overseaschinese_add")
    public String overseaschinese_add(){
        return "OverseasChinese/overseaschinese-add";
    }
    @RequestMapping(value = "/overseaschinese_add.do")
    @ResponseBody
    public String overseaschinese_add_do(HttpServletRequest request, @RequestParam(value = "picture",required = false)MultipartFile[] pictures,
                                         String statistics, String conferences, int huaqiao_number, String census_time, String immigrant_history,
                                         String huaqiao_association, String chinese_media, String chinese_school, String country_code, String refrence){

        VOverseasChinese VOverseasChinese =new VOverseasChinese(statistics,conferences,huaqiao_number,census_time,immigrant_history,
                huaqiao_association,chinese_media,chinese_school,country_code,refrence);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        VOverseasChinese.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VOverseasChinese.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.OVERSEASCHINESE;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.OVERSEASCHINESE));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VOverseasChinese.setPicture(picture_path);
            }

            judge=OverseasService.insert(VOverseasChinese);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败，请检查国家代码";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request, ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VOverseasChinese> elist=OverseasService.findByCondition(map);
        mmap.put("olist",elist);
        return "OverseasChinese/overseaschinese";
    }
    @RequestMapping(value = "/overseaschinese_delete")
    @ResponseBody
    public String baseinfo_delete(String country_code){
        Boolean judge=null;
        try{
            judge=OverseasService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
